package cn.com.lms.business.course.web;

import cn.com.lms.business.core.constant.BNZConstants;
import cn.com.lms.business.course.service.FbCourseInfoService;
import cn.com.lms.business.learning.service.LearningPlanService;
import com.bnzj.core.rest.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/*
 * @author liuwei
 * @date 2020/3/23 11:25
 * @desc APP我的课程API
 */
@Tag(name = "课程相关", description = "APP我的课程")
@RestController
@RequestMapping("/app/course")
public class CourseInfoAppController {

    @Autowired
    private FbCourseInfoService courseInfoService;

    @Autowired
    private LearningPlanService planService;

    @GetMapping("/queryCourseList")
    @Operation(summary = "我的课程列表", parameters = {
            @Parameter(name = "title", description = "课程标题", allowReserved = true, schema = @Schema(type = "String", description = "课程标题")),
            @Parameter(name = "courseTypeId", description = "课程类型", allowReserved = true, schema = @Schema(type = "String", description = "课程类型")),
            @Parameter(name = "limit", description = "分页大小", allowReserved = true, schema = @Schema(type = "int", description = "分页大小")),
            @Parameter(name = "offset", description = "页码", allowReserved = true, schema = @Schema(type = "int", description = "页码"))})
    public ResponseResult queryCourseList(HttpServletRequest request) {
        return courseInfoService.queryCourseListByUser(request);
    }

    /*
     * @author liuwei
     * @date 2020/3/16 18:39
     * @desc 获取课程类型
     */
    @Operation(summary = "获取课程类型")
    @GetMapping("/getDictionaryList")
    public ResponseResult<List<Map<String, Object>>> getDictionaryList() {
        return ResponseResult.success(courseInfoService.getCourseTypeDictionary(BNZConstants.COURSE_TYPE_CODE));
    }


    @GetMapping("/getCourseView/{courseId}")
    @Operation(summary = "课程详情", parameters = {@Parameter(name = "planId", description = "计划ID")})
    public ResponseResult getCourseView(HttpServletRequest request, @Parameter(name = "courseId", description = "课程id") @PathVariable(value = "courseId") Long courseId) {
        //记录学员已参加过计划记录
        if (null != request.getParameter("planId")) {
            planService.savePlanJoinRecord(Long.valueOf(request.getParameter("planId")));
        }
        return courseInfoService.getCourseView(courseId);
    }


    @PostMapping("/updateCourse")
    @Operation(summary = "更新课程学时信息")
    public ResponseResult updateCourse(@Parameter(name = "detailId", description = "详情Id") Long detailId, @Parameter(name = "learningHours", description = "已完成学时") Integer learningHours) {
        return courseInfoService.updateCourse(detailId, learningHours);
    }

}
